package com.ajou.adonate.imap;

import com.ajou.adonate.database.DatabaseManager;
import com.google.code.samples.xoauth.XoauthAuthenticator;

public class MailReceiver {
	String mEmail;
	String mMovieId;
	String mHash;
	String mOAuthToken;
	String mOAuthTokenSecret;
	String mRetPath;
	
//	String mDownloadPath = "/Users/tapaha7/imap/";
	String mDownloadPath = "";
	
	DatabaseManager d = null;
	
	public MailReceiver(String movie_id) {
		mMovieId = movie_id;
		
		d = new DatabaseManager();
		try {
			Class.forName(d.getJdbcDriver());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public String receive() {
		mEmail = d.getEmailByMovieId(mMovieId);
		mHash = d.getHashByMovieId(mMovieId);
		mOAuthToken = d.getOAuthTokenByMovieId(mMovieId);
		mOAuthTokenSecret = d.getOAuthTokenSecretByMovieId(mMovieId);
		
		XoauthAuthenticator x = new XoauthAuthenticator();
		
		try {
			mRetPath = x.getMessage(mDownloadPath, mEmail, mOAuthToken, mOAuthTokenSecret, mHash);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		System.out.println("Return Path : " + mRetPath);
		
		return mRetPath;
	}
}
